﻿//SecurityIdGenerator.cs
//Copyright (c) 2013 StockSharp LLC, all rights reserved.
//This code module is part of StockSharp library.
//This code is licensed under the GNU GENERAL PUBLIC LICENSE Version 3.
//See the file License.txt for the license details.
//More info on: http://stocksharp.com

namespace StockSharp.Algo
{
	using System;

	using Ecng.Common;

	using StockSharp.BusinessEntities;

	/// <summary>
	/// Генератор идентификаторов инструментов <see cref="Security.Id"/>.
	/// </summary>
	public class SecurityIdGenerator
	{
		/// <summary>
		/// Создать <see cref="SecurityIdGenerator"/>.
		/// </summary>
		public SecurityIdGenerator()
		{
			Delimiter = "@";
		}

		/// <summary>
		/// Разделитель между кодом и классом инструмента.
		/// </summary>
		public string Delimiter { get; set; }

		/// <summary>
		/// Сгенерировать <see cref="Security.Id"/> инструмента.
		/// </summary>
		/// <param name="secCode">Код инструмента.</param>
		/// <param name="secClass">Класс инструмента.</param>
		/// <param name="board">Площадка инструмента.</param>
		/// <returns><see cref="Security.Id"/> инструмента.</returns>
		public virtual string GenerateId(string secCode, string secClass, ExchangeBoard board)
		{
			if (secCode.IsEmpty())
				throw new ArgumentNullException("secCode");

			if (board == null)
				throw new ArgumentNullException("board");

			if (board == ExchangeBoard.Forts)
				secClass = "RTS";
			else if (board == ExchangeBoard.Ux)
				secClass = "UX";

			if (secClass.IsEmpty())
				secClass = board.Code;

			return secCode + Delimiter + secClass;
		}

		/// <summary>
		/// Получить код, класс и информацию о бижре для инструмента по его идентификатору.
		/// </summary>
		/// <param name="securityId">Идентификатор инструмента <see cref="Security.Id"/>.</param>
		/// <returns>Код инструмента <see cref="Security.Code"/>, его класс <see cref="Security.Class"/> и информация о бирже <see cref="Security.ExchangeBoard"/>.</returns>
		public virtual Tuple<string, string, ExchangeBoard> GetInfo(string securityId)
		{
			if (securityId.IsEmpty())
				throw new ArgumentNullException("securityId");

			var parts = securityId.Split(Delimiter);

			if (parts.Length != 2)
				return new Tuple<string, string, ExchangeBoard>(securityId, string.Empty, ExchangeBoard.Test);

			var exchange = ExchangeBoard.Micex;

			if (parts[1].CompareIgnoreCase("RTS"))
				exchange = ExchangeBoard.Forts;
			else if (parts[1].CompareIgnoreCase("UX"))
				exchange = ExchangeBoard.Ux;

			return new Tuple<string, string, ExchangeBoard>(parts[0], parts[1], exchange);
		}
	}
}